home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / old-mac-specific.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  33.4 KB  |  1,362 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <PrintTraps.h>
  6. #include <MacProto.h>
  7. #include "mac-specific.h"
  8. #include "mac_printing.h"
  9. #include "dvihead.h"
  10. #include "gendefs.h"
  11. #include "egblvars.h"
  12. #include "m72.h"
  13. #include "e_bitmap.h"
  14. #include "TextDisplay.h"
  15.  
  16. #define PROFILE 0
  17.  
  18. #define REVEAL_DIALOG(d) {SelectWindow(d);ShowWindow(d);}
  19. #define MOVE_TO_FRONT        -1L
  20. #define MIN_SLEEP            3L
  21. #define NIL_MOUSE_REGION    0L
  22. #define NIL_POINTER            0L
  23.  
  24. #define WNE_TRAP_NUM        0x60
  25. #define UNIMPL_TRAP_NUM        0x9F
  26.  
  27. #define APPLE_MENU_ID        1
  28. #define FILE_MENU_ID        2
  29. #define CONFIGURE_MENU_ID    3
  30.  
  31. #define ABOUT_ITEM        1
  32.  
  33. #define DRAG_THRESHOLD    30
  34. #define IDLE_INTERVAL    10    /* Min. interval in ticks between printer idle stuff */
  35.  
  36. /* ------------- global variables ----------------- */
  37. DialogPtr            cancel_dialog, configure_dialog;
  38. Handle            cancel_dialog_text;
  39. EventRecord        g_TheEvent;
  40. Rect                 g_DragRect, g_screenrect;
  41. MenuHandle        g_AppleMenu, g_FileMenu, g_ConfigureMenu;
  42. Boolean            g_Done, g_WNEImplemented;
  43. Boolean            g_abort_dvi; /* error cancels this dvi file */
  44. int                g_dpi;    /* bitmap resolution */
  45. int                g_printer_dpi;    /* device resolution */
  46. int                g_print_res_file;
  47. int                g_app_resfile;
  48. GrafPort        g_offscreen_GrafPort;
  49. WindowPtr        g_page_window,
  50.                 g_console_window;
  51. THPrint            g_print_rec_h = NIL_POINTER;
  52. Str255            g_other_commands;
  53. WindowPtr        g_freemem_window;
  54. MenuHandle        g_popup_menu;
  55. Boolean            g_use_sysheap;
  56. long                g_last_idle_time = 0L;
  57.  
  58. /* -------------- prototypes of private routines ------------ */
  59. void    show_about        ( void );
  60. void    Show_warning(     char *msg );
  61. void    init_toolbox    ( void );
  62. int        Get_resource_id(
  63.                         OSType rsrc_type,
  64.                         char *rsrc_name);
  65. Handle Get_resource_by_id( OSType rsrc_type, int r_id );
  66. void    Get_resource(                /* find a resource by name */
  67.                         OSType resource_type,
  68.                         char *resource_name,
  69.                         Handle *resource_handle,
  70.                         int *resource_id);
  71. void    Get_file_path(     char *thepath );
  72. Boolean    User_wants_out    ( void );
  73. void    init_per_dvi(    void );
  74. pascal Boolean
  75.         Dvi_filter(     ParmBlkPtr param_block );
  76. void    Get_dpi(        void );
  77. Boolean Get_event(        int mask,
  78.                         EventRecord *what);
  79. void    Get_dialogs(    void );
  80. void    Init_offscreen(    void );
  81. Point    Get_SF_place(    int rsrc_id );
  82. void    Setup_drag_rect(    void );
  83. void    Do_update(        WindowPtr  where );
  84. void    Handle_print(    Boolean use_job_dialog );
  85. void    Handle_event( void );
  86. void    Do_page_setup(    void );
  87. void    Init_print_rec(    void );
  88. void    Set_draft_bits(    Boolean draft_bits );
  89. void    Open_printer(    Boolean use_job_dialog,
  90.                         int *first,
  91.                         int *last );
  92. void    HandleFileChoice( int theItem );
  93. Boolean    get_bool_resource( char *rsrc_name );
  94. int        get_int_resource( char *rsrc_name );
  95. void        set_int_resource( char *rsrc_name, int new_value );
  96. void    set_str_resource(
  97.                         char *rsrc_name,
  98.                         char *new_value );
  99. char **
  100.         get_str_resource( char *rsrc_name );
  101.  
  102. /* -------------------------------------------------------------------- */
  103.  
  104. /* ----- a couple of prototypes of external routines ------ */
  105. void    HandleConfigureChoice( int theItem );
  106. void    Do_configure    ( void );
  107. void    dvifile( char *filename );
  108. void    option( char *an_option );
  109. Boolean    setupbeachball();
  110. void    rollbeachball();
  111. pascal void    Printer_idle();
  112. void    Reposition_windows( void );
  113. void    Save_window_positions( void );
  114. void    Center_window(    WindowPtr my_window );
  115. void    Explain_print_error( OSErr the_error );
  116. void    Update_freemem( void );
  117. void    Update_memory_indicators();
  118. void    openfont( char *fontname ); /* only used here for UnloadSeg */
  119. void    special( char *s ); /* only used here for UnloadSeg */
  120. ControlHandle    Get_Ditem_handle( DialogPtr theDialog, int item_no );
  121. pascal void Get_PICT_data( Ptr data_ptr, int byte_count );
  122.  
  123. /* ----------------------------- main ------------------------------ */
  124. void
  125. main()
  126. {
  127.     init_toolbox();
  128.     (void) strcpy(g_progname, "DVIM72-Mac");
  129.     g_app_resfile = CurResFile();
  130.     Setup_drag_rect();
  131.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  132.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  133.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  134.     Get_dialogs();
  135.     Reposition_windows();
  136.     printf( "\n\n\n\nWelcome to DVIM72-Mac.\n" );
  137.     if (get_bool_resource("FREEMEM"))
  138.         REVEAL_DIALOG( g_freemem_window );
  139.     g_use_sysheap = get_bool_resource("sysheap");
  140.     Get_dpi();
  141.     Init_offscreen();
  142.     (void) initglob();
  143.     Clear_bitmap();
  144.     REVEAL_DIALOG( g_page_window );
  145.     Init_menubar();
  146.     Init_print_rec();
  147.     Update_freemem();
  148.     InitCursor();
  149.     (void) setupbeachball();
  150.     MainLoop();
  151.     Save_window_positions();
  152.     TD_close_log();
  153. }
  154.  
  155. /* ------------------------ Setup_drag_rect ----------------- */
  156. void
  157. Setup_drag_rect()
  158. {
  159.     SysEnvRec    the_world;
  160.     OSErr             err;
  161.  
  162.     err = SysEnvirons(1, &the_world );
  163.     if ( (err != noErr) || !the_world.hasColorQD )
  164.     {
  165.         g_screenrect = screenBits.bounds;
  166.     }
  167.     else
  168.         g_screenrect = (**GetMainDevice()).gdRect;
  169.     if (EmptyRect(&g_screenrect))
  170.         fatal("Can't get the screen rectangle!\n");
  171.     g_screenrect.top += GetMBarHeight();
  172.     g_DragRect = g_screenrect;
  173.     InsetRect( &g_DragRect, DRAG_THRESHOLD, DRAG_THRESHOLD );
  174.     g_DragRect.top -= DRAG_THRESHOLD;
  175. }
  176.  
  177.  
  178. /* ------------------------- Init_offscreen ---------------- */
  179. void
  180. Init_offscreen()
  181. {
  182.     GrafPtr save_port;
  183.     char dpi_str[5];
  184.     QDProcsPtr    my_procs;
  185.     
  186.     bitmap = (UNSIGN32*) NewPtr(
  187.         ((UNSIGN32)XBIT)*((UNSIGN32)YBIT)*sizeof(UNSIGN32));
  188.     if (bitmap == (UNSIGN32*)NULL)
  189.     {
  190.         Show_error("Init_offscreen: cannot allocate enough"
  191.             " space for the page image bitmap.  Reducing"
  192.             " resolution to 72dpi.");
  193.         g_dpi = 72;
  194.         set_int_resource( "BITMAP DPI", g_dpi );
  195.         bitmap = (UNSIGN32*) NewPtr(
  196.             ((UNSIGN32)XBIT)*((UNSIGN32)YBIT)*sizeof(UNSIGN32));
  197.         if (bitmap == (UNSIGN32*)NULL)
  198.             (void)fatal(
  199.                 "Init_offscreen():  Cannot allocate space"
  200.                 " for page image bitmap");
  201.     }
  202.     
  203.     GetPort( &save_port );
  204.     OpenPort( &g_offscreen_GrafPort );
  205.     g_offscreen_GrafPort.portBits.rowBytes = XWORDS * 4;
  206.     SetRect( &g_offscreen_GrafPort.portBits.bounds,
  207.         0, 0, XSIZE, YSIZE );
  208.     g_offscreen_GrafPort.portBits.baseAddr = (Ptr) bitmap;
  209.     g_offscreen_GrafPort.portRect =
  210.         g_offscreen_GrafPort.portBits.bounds;
  211.     /*
  212.         The default visRgn is screenBits.bounds, which is
  213.         too small for printing.
  214.     */
  215.     DisposeRgn( g_offscreen_GrafPort.visRgn );
  216.     g_offscreen_GrafPort.visRgn = g_offscreen_GrafPort.clipRgn;
  217.     
  218.     /* To support PICT \specials, we replace the getPicProc bottleneck routine. */
  219.     my_procs = (QDProcsPtr) NewPtr( sizeof(QDProcs) );
  220.     if (my_procs != 0)
  221.     {
  222.         SetStdProcs( my_procs );
  223.         my_procs->getPicProc = (Ptr)&Get_PICT_data;
  224.         g_offscreen_GrafPort.grafProcs = my_procs;
  225.     }
  226.     
  227.     SetPort( save_port );
  228. }
  229.  
  230. /* ------------------- Clear_bitmap ------------------------ */
  231. void
  232. Clear_bitmap()
  233. {
  234.     GrafPtr save_port;
  235.     
  236.     GetPort( &save_port );
  237.     SetPort( &g_offscreen_GrafPort );
  238.     EraseRect( &g_offscreen_GrafPort.portRect );
  239.     SetPort( save_port );
  240. }
  241.  
  242. /* ---------------------------- Get_dialogs ------------------------- */
  243. void
  244. Get_dialogs()
  245. {
  246.     GrafPtr save_port;
  247.     int cancel_DLOG_id;
  248.     int WIND_id;
  249.     OSErr    stat;
  250.     
  251.     GetPort( &save_port );
  252.  
  253.     cancel_DLOG_id = Get_resource_id( 'DLOG', "cancel print" );
  254.     CouldDialog( cancel_DLOG_id );
  255.     cancel_dialog = GetNewDialog( cancel_DLOG_id,
  256.         nil, (WindowPtr)MOVE_TO_FRONT );
  257.     Center_window( cancel_dialog );
  258.     cancel_dialog_text = (Handle)Get_Ditem_handle( cancel_dialog, 1 );
  259.     
  260.     WIND_id = Get_resource_id( 'WIND', "console" );
  261.     g_console_window = TD_new( WIND_id, 5000,  4000, 9, &stat );
  262.     if (stat != noErr)
  263.         fatal("Error opening console window.");
  264.  
  265.     WIND_id = Get_resource_id( 'WIND', "FreeMem" );
  266.     g_freemem_window = GetNewWindow( WIND_id,
  267.         nil, (WindowPtr)MOVE_TO_FRONT );
  268.  
  269.     WIND_id = Get_resource_id( 'WIND', "page" );
  270.     g_page_window = GetNewWindow( WIND_id,
  271.         nil, (WindowPtr)MOVE_TO_FRONT );
  272.     
  273.     SetPort( save_port );
  274. }
  275.  
  276. /* --------------------------- Get_dpi ----------------------------- */
  277. void
  278. Get_dpi()
  279. {
  280.     g_dpi = get_int_resource("BITMAP DPI");
  281.  
  282.     g_printer_dpi = get_int_resource("PRINTER DPI");
  283. }
  284.  
  285.  
  286. /* ------------------------ MainLoop ------------------- */
  287.  
  288. MainLoop()
  289. {
  290.     g_Done = FALSE;
  291.     g_WNEImplemented = ( NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  292.         NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap) );
  293.     while ( g_Done == FALSE )
  294.     {
  295.         Handle_event();
  296.     }
  297.     /* fflush(stdout); */
  298. }
  299.  
  300. #define    RESUME_MASK            1
  301. /* ----------------- Handle_event ----------------- */
  302.  
  303. void Handle_event()
  304. {
  305.     char theChar;
  306.  
  307.     (void)Get_event( everyEvent, &g_TheEvent );
  308.     
  309.     switch (g_TheEvent.what)
  310.     {
  311. #if NEVER
  312.         case nullEvent:
  313.             /* HandleNull(); */
  314.             break;
  315. #endif
  316.         case mouseDown:
  317.             HandleMouseDown();
  318.             break;
  319.         case keyDown:
  320.         case autoKey:
  321.             theChar = g_TheEvent.message & charCodeMask;
  322.             if ( (g_TheEvent.modifiers & cmdKey) != 0 )
  323.             {
  324.                 if ( (theChar == '.') && ((WindowPeek)cancel_dialog)->visible )
  325.                 {
  326.                     g_abort_dvi = TRUE;
  327.                     PrSetError( iPrAbort );
  328.                     SetIText( cancel_dialog_text, "\pCancelling..." );
  329.                     DrawDialog( cancel_dialog );
  330.                 }
  331.                 else
  332.                     HandleMenuChoice( MenuKey( theChar ) );
  333.             }
  334.             else
  335.                 SysBeep(1);
  336.             break;
  337.         case updateEvt:
  338.             Do_update( (WindowPtr)g_TheEvent.message );
  339.             break;
  340.         case activateEvt:
  341.             SetPort( (WindowPtr)g_TheEvent.message );
  342.             if (g_TheEvent.modifiers & activeFlag) /* activate event */
  343.                 TD_activate( TRUE );
  344.             else
  345.                 TD_activate( FALSE );
  346.             break;
  347.         case app4Evt:
  348.             SetPort( FrontWindow() );
  349.             if (g_TheEvent.message & RESUME_MASK)
  350.                 TD_activate( TRUE );
  351.             else
  352.                 TD_activate( FALSE );
  353.             break;
  354.     }
  355. }
  356.  
  357. /* ------------------- Do_Update -------------------------- */
  358. void
  359. Do_update( where )
  360.     WindowPtr where;
  361. {
  362.     Rect    from_rect;
  363.     GrafPtr    save_port;
  364.     
  365.     if ( !IsDAWindow( where ) )
  366.     {
  367.         GetPort( &save_port );
  368.         SetPort( (GrafPtr)where );
  369.         BeginUpdate( where );
  370.         if (where == g_page_window)
  371.         {
  372.             from_rect = g_offscreen_GrafPort.portRect;
  373.             if (g_printer_dpi < g_dpi)
  374.             {
  375.                 from_rect.right = (int)((long)from_rect.right *
  376.                     (long)g_printer_dpi / g_dpi);
  377.                 from_rect.bottom = (int)((long)from_rect.bottom *
  378.                     (long)g_printer_dpi / g_dpi);
  379.             }
  380.             CopyBits( &g_offscreen_GrafPort.portBits,
  381.                 &g_page_window->portBits,
  382.                 &from_rect,
  383.                 &g_page_window->portRect,
  384.                 srcCopy, nil );
  385.             DrawGrowIcon( where );
  386.         }
  387.         else if (where == g_freemem_window)
  388.             Update_freemem();
  389.         else if (where == cancel_dialog)
  390.             DrawDialog( cancel_dialog );
  391.         else if (where == g_console_window)
  392.         {
  393.             DrawControls( where );
  394.             TD_update( );
  395.         }
  396.         EndUpdate( where );
  397.         SetPort( save_port );
  398.     }
  399. }
  400.  
  401. /* ----------------- HandleMouseDown ----------------- */
  402.  
  403. HandleMouseDown()
  404. {
  405.     WindowPtr    whichWindow;
  406.     int            thePart;
  407.     long        menuChoice, windSize;
  408.     long new_size;
  409.     
  410.     thePart = FindWindow( g_TheEvent.where, &whichWindow );
  411.     switch (thePart)
  412.     {
  413.         case inMenuBar:
  414.             menuChoice = MenuSelect( g_TheEvent.where );
  415.             HandleMenuChoice( menuChoice );
  416.             break;
  417.         case inSysWindow:
  418.             SystemClick( &g_TheEvent, whichWindow );
  419.             break;
  420.         case inDrag:
  421.             SelectWindow( whichWindow );
  422.             DragWindow( whichWindow, g_TheEvent.where, &g_DragRect );
  423.             break;
  424.         case inContent:
  425.             SelectWindow( whichWindow );
  426.             SetPort( whichWindow );
  427.             if (whichWindow == g_console_window)
  428.                 TD_click(g_TheEvent.where);
  429.             else
  430.                 DragWindow( whichWindow, g_TheEvent.where, &g_DragRect );
  431.             break;
  432.         case inGrow:
  433.             SelectWindow( whichWindow );
  434.             new_size = GrowWindow( whichWindow, g_TheEvent.where,
  435.                 &g_DragRect );
  436.             if (new_size)
  437.             {
  438.                 SizeWindow( whichWindow, LoWord(new_size),
  439.                     HiWord(new_size), TRUE );
  440.                 SetPort( whichWindow );
  441.                 TD_resize();
  442.                 InvalRect( &whichWindow->portRect );
  443.             }
  444.     }
  445. }
  446.  
  447. /* -------------------- HandleMenuChoice ----------------------- */
  448.  
  449. HandleMenuChoice( menuChoice )
  450. long  menuChoice;
  451. {
  452.     int  theMenu, theItem;
  453.     
  454.     if ( menuChoice != 0 )
  455.     {
  456.         theMenu = HiWord( menuChoice );
  457.         theItem = LoWord( menuChoice );
  458.         switch( theMenu )
  459.         {
  460.             case APPLE_MENU_ID:
  461.                 HandleAppleChoice( theItem );
  462.                 break;
  463.             case FILE_MENU_ID:
  464.                 HandleFileChoice( theItem );
  465.                 break;
  466.             case CONFIGURE_MENU_ID:
  467.                 HandleConfigureChoice( theItem );
  468.                 UnloadSeg( HandleConfigureChoice );
  469.                 break;
  470.         }
  471.         HiliteMenu( 0 );
  472.     }
  473. }
  474.  
  475. /* ------------------- HandleAppleChoice ------------------- */
  476.  
  477. HandleAppleChoice( theItem )
  478. int  theItem;
  479. {
  480.     Str255        accName;
  481.     int            accNumber;
  482.     
  483.     switch( theItem )
  484.     {
  485.         case ABOUT_ITEM:
  486.             show_about();
  487.             break;
  488.         default:
  489.             GetItem( g_AppleMenu, theItem, accName );
  490.             accNumber = OpenDeskAcc( accName );
  491.             break;
  492.     }
  493. }
  494.  
  495. #define USE_JOB_DIALOG    1
  496. #define NO_JOB_DIALOG    0
  497. /* --------------------- HandleFileChoice ------------------- */
  498. typedef enum {
  499.     PAGE_SETUP_ITEM = 1,
  500.     /* ---- */
  501.     PRINT_ITEM = 3,
  502.     JUST_PRINT_ITEM,
  503.     /* ----- */
  504.     QUIT_ITEM = 6
  505. };
  506.  
  507. void
  508. HandleFileChoice( theItem )
  509. int    theItem;
  510. {
  511.     switch( theItem )
  512.     {
  513.         case PAGE_SETUP_ITEM:
  514.             Do_page_setup();
  515.             break;
  516.         case PRINT_ITEM:
  517.             Handle_print( USE_JOB_DIALOG );
  518.             break;
  519.         case JUST_PRINT_ITEM:
  520.             Handle_print( NO_JOB_DIALOG );
  521.             break;
  522.         case QUIT_ITEM:
  523.             g_Done = TRUE;
  524.             break;
  525.     }
  526. }
  527.  
  528.  
  529. /* ---------------------- IsDAWindow ------------------------ */
  530.  
  531. IsDAWindow( whichWindow )
  532. WindowPtr  whichWindow;
  533. {
  534.     if ( whichWindow == NIL_POINTER )
  535.         return( FALSE );
  536.     else
  537.         /* DA windows have negative WindowKinds  */
  538.         return( ( ((WindowPeek)whichWindow) -> windowKind ) < 0 );
  539. }
  540.  
  541.  
  542. /* ------------------- Get_event ------------------ */
  543. Boolean
  544. Get_event( int event_mask, EventRecord *what_event )
  545. {
  546.     register  Boolean val;
  547.     
  548.     if ( g_WNEImplemented )
  549.         val = WaitNextEvent( event_mask, what_event, MIN_SLEEP,
  550.             NIL_MOUSE_REGION );
  551.     else
  552.     {
  553.         SystemTask();
  554.         val = GetNextEvent( event_mask, what_event );
  555.     }
  556.     return( val );
  557. }
  558.  
  559.  
  560. /* ----------------------- Init_menubar ------------------- */
  561.  
  562. Init_menubar()
  563. {
  564.     int MBAR_id;
  565.     Handle my_bar;
  566.     int popup_id;
  567.     
  568.     MBAR_id = Get_resource_id( 'MBAR', "bar" );
  569.     my_bar = GetNewMBar( MBAR_id );
  570.     SetMenuBar( my_bar );
  571.     g_AppleMenu = GetMHandle( APPLE_MENU_ID );
  572.     g_FileMenu = GetMHandle( FILE_MENU_ID );
  573.     g_ConfigureMenu = GetMHandle( CONFIGURE_MENU_ID );
  574.     AddResMenu( g_AppleMenu, 'DRVR' );
  575.     DrawMenuBar();
  576.     
  577.     popup_id = Get_resource_id( 'MENU', "popup" );
  578.     g_popup_menu = GetMenu( popup_id );
  579.     InsertMenu( g_popup_menu, -1 );
  580. }
  581.  
  582. /* --------------------- Handle_print --------------------- */
  583. void
  584. Handle_print( Boolean use_job_dialog )
  585. {
  586.     char    t_str[256], t_str2[256];
  587.     char    dvi_file[256];
  588.     char    option_string[256];
  589.     char    *t_ptr;
  590.     int        first_page, last_page;
  591.     
  592.     g_abort_dvi = FALSE;
  593.     Get_file_path( dvi_file );
  594.     if (strlen(dvi_file) == 0)        /* user chose "cancel" in SF dialog */
  595.         return;
  596.     
  597.     /* Initialize defaults */
  598.     init_per_dvi();
  599.     
  600.     Open_printer( use_job_dialog, &first_page, &last_page );
  601.     if (g_abort_dvi)
  602.         return;  /* Error or else user clicked Cancel */
  603.     
  604.     /* Get info out of options dialog send to option() */
  605.     if ( (first_page != 1) || (last_page != iPrPgMax) )
  606.     {
  607.         (void)sprintf( t_str, "%d", first_page );
  608.         (void)sprintf( t_str2, "%d", last_page );
  609.         (void) strcpy(option_string, "-o");
  610.         (void) strcat( option_string, t_str );
  611.         (void) strcat( option_string, ":" );
  612.         (void) strcat( option_string, t_str2 );
  613.         option( option_string );
  614.     }
  615.     rollbeachball();
  616.     (void) PtoCstr( (char *)g_other_commands );
  617.     if (strlen((char *)g_other_commands))
  618.     {
  619.         t_ptr = strtok( (char *)g_other_commands, " \t" );
  620.         while( t_ptr != NULL )
  621.         {
  622.             option( t_ptr );
  623.             t_ptr = strtok( NULL, " \t" );
  624.         }
  625.     }
  626.     
  627.     /* Send offset info from config to option() */
  628.     t_ptr = *get_str_resource( "HOFFSET" );
  629.     if (strlen(t_ptr))
  630.     {
  631.         (void)strcpy( option_string, "-x" );
  632.         (void)strcat( option_string, t_ptr );
  633.         option( option_string );
  634.     }
  635.     t_ptr = *get_str_resource( "VOFFSET" );
  636.     if (strlen(t_ptr))
  637.     {
  638.         (void)strcpy( option_string, "-y" );
  639.         (void)strcat( option_string, t_ptr );
  640.         option( option_string );
  641.     }
  642.     if ( get_bool_resource( "REVPAGEORDER" ) )
  643.         option( "-b" );
  644.     
  645.     if (npage == 0)        /* no page ranges given, make a large one */
  646.     {
  647.         page_begin[0] = 1;
  648.         page_end[0] = 32767;    /* arbitrary large integer */
  649.         page_step[0] = 1;
  650.         npage = 1;
  651.     }
  652.  
  653.     if (! g_abort_dvi)
  654.     {
  655.         DisableItem( g_FileMenu, 0 );    /* disable entire File menu */
  656.         DisableItem( g_ConfigureMenu, 0 );    /* disable entire Configure menu */
  657.         DrawMenuBar();
  658.         dvifile( dvi_file );    /* go print it! */;
  659.         InitCursor();
  660.         Update_memory_indicators();
  661.         EnableItem( g_FileMenu, 0 );    /* enable entire File menu */
  662.         EnableItem( g_ConfigureMenu, 0 );    /* enable entire Configure menu */
  663.         DrawMenuBar();
  664.     }
  665. }
  666.  
  667. /* ----------------- Init_print_rec() -------------------- */
  668. void
  669. Init_print_rec()
  670. {
  671.     TSetRslBlk set_resolution_block;
  672.     Boolean    t_draft;
  673.     
  674.     g_print_rec_h = (THPrint) NewHandle( sizeof(TPrint) );
  675.     if ( (g_print_rec_h == nil) || (MemError() != noErr) )
  676.     {
  677.         Show_error("Init_print_rec: NewHandle error.");
  678.         alldone();
  679.     }
  680.     PrOpen();
  681.     PrintDefault( g_print_rec_h );
  682.     switch ( get_int_resource("PAPER FEED") )
  683.     {
  684.         case feedCut:
  685.             (**g_print_rec_h).prStl.feed = feedCut;
  686.             break;
  687.         case feedFanfold:
  688.             (**g_print_rec_h).prStl.feed = feedFanfold;
  689.             break;
  690.         default:
  691.             break;    /* Use the driver's default */
  692.     }
  693.     switch ( get_int_resource("SPOOL") )
  694.     {
  695.         case bDraftLoop:
  696.             (**g_print_rec_h).prJob.bJDocLoop = bDraftLoop;
  697.             t_draft = TRUE;
  698.             break;
  699.         case bSpoolLoop:
  700.             (**g_print_rec_h).prJob.bJDocLoop = bSpoolLoop;
  701.             t_draft = FALSE;
  702.             break;
  703.         default:
  704.             t_draft = FALSE;
  705.             break;
  706.     }
  707.     Set_draft_bits( t_draft );
  708.  
  709.     set_resolution_block.iOpCode = setRslOp;
  710.     set_resolution_block.hPrint = g_print_rec_h;
  711.     set_resolution_block.iXRsl = g_printer_dpi;
  712.     set_resolution_block.iYRsl = g_printer_dpi;
  713.     PrGeneral( &set_resolution_block ); /* set device resolution */
  714.     if ((PrError() != noErr) || set_resolution_block.iError)
  715.     {
  716.         Show_warning("Init_print_rec: PrGeneral resolution error.");
  717.     }
  718.  
  719.     PrValidate( g_print_rec_h );
  720.     PrClose();
  721. }
  722.  
  723. /* ------------------ Set_draft_bits ---------------------- */
  724. void
  725. Set_draft_bits( Boolean draft_bits )
  726. /* 
  727.     Call PrGeneral to turn draft-mode bitmap printing on or off.
  728.     The calling program should have called PrOpen.
  729. */
  730. {
  731.     TDftBitsBlk draft_bits_block;
  732.     
  733.     draft_bits_block.iOpCode = draft_bits?
  734.         draftBitsOp : noDraftBitsOp;
  735.     draft_bits_block.hPrint = g_print_rec_h;
  736.     PrGeneral( &draft_bits_block );
  737. }
  738.  
  739. /* ------------------ Do_page_setup ----------------------- */
  740. void
  741. Do_page_setup()
  742. {
  743.     PrOpen();
  744.     PrStlDialog( g_print_rec_h );
  745.     PrClose();
  746. }
  747.  
  748. /* ----------------- Open_printer --------------------- */
  749.  
  750. void Open_printer( Boolean use_job_dialog,
  751.     int *first_page, int *last_page )
  752. {
  753.     char *s_ptr;
  754.     OSErr err;
  755.     
  756.     UnloadSeg( sprintf );    /* get rid of ANSI segment */
  757.     UnloadSeg( special );    /* maybe we won't need it */
  758.     PrOpen();
  759.     if (PrError() != noErr)
  760.     {
  761.         Show_error("Open_printer: PrOpen error.");
  762.         g_abort_dvi = TRUE;
  763.         PrClose();
  764.         return;
  765.     }
  766.     g_print_res_file = CurResFile();
  767.     
  768.     if (use_job_dialog == USE_JOB_DIALOG)
  769.     {
  770.         err = Special_job_dialog( g_print_rec_h );
  771.         if (err != noErr)
  772.         {
  773.             g_abort_dvi = TRUE;
  774.             PrClose();
  775.             return;
  776.         }
  777.     }
  778.     else
  779.     {
  780.         (**g_print_rec_h).prJob.iFstPage = 1;
  781.         (**g_print_rec_h).prJob.iLstPage = iPrPgMax;
  782.         (**g_print_rec_h).prJob.iCopies = 1;
  783.     }
  784.     
  785.     *first_page = (**g_print_rec_h).prJob.iFstPage;
  786.     *last_page = (**g_print_rec_h).prJob.iLstPage;
  787.     (**g_print_rec_h).prJob.iFstPage = 1;
  788.     (**g_print_rec_h).prJob.iLstPage = iPrPgMax;
  789.     
  790.     if (PrValidate(g_print_rec_h))
  791.         printf("PrValidate changed something!\n");
  792.  
  793.     UseResFile(g_print_res_file);
  794.     rollbeachball();
  795.     g_print_port_p = PrOpenDoc( g_print_rec_h, nil, nil );
  796.     if (PrError() != noErr)
  797.     {
  798.         Show_error("Open_printer: PrOpenDoc error.");
  799.         g_abort_dvi = TRUE;
  800.         return;
  801.     }
  802.  
  803.     /* Set up the Cancel dialog */
  804.     if ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  805.         SetIText( cancel_dialog_text, "\pNow Spooling." );
  806.     else
  807.         SetIText( cancel_dialog_text, "\pNow Printing." );
  808.     REVEAL_DIALOG( cancel_dialog );
  809.     DrawDialog( cancel_dialog );
  810.     
  811.     Update_freemem();
  812. }
  813.  
  814. /* -------------------- Print_page -------------------- */
  815.  
  816. void Print_page()
  817. {
  818.     GrafPtr    save_port_p;
  819.     Rect        dest_rect; /* = {0, 0, 752, 576} at 72dpi */
  820.     Rect        origin_rect;
  821.     int i, strip_depth, page_top, page_bottom;
  822.     long n;
  823.     float        scale;
  824.     int        int_scale;
  825.     
  826.     GetPort( &save_port_p );
  827.     /* (void)fflush(stdout); */
  828.     
  829.     if (g_printer_dpi < g_dpi)    /* scale before printing */
  830.     {
  831.         scale = (float)g_dpi / (float)g_printer_dpi;
  832.         int_scale = (int)(scale + 0.5);    /* round to nearest integer */
  833.         if ( ABS(scale - (float)int_scale) < 0.1)    /* scale is nearly an integer */
  834.         {
  835.                 SetRect( &dest_rect, 0, 0, 
  836.                     g_offscreen_GrafPort.portRect.right / int_scale,
  837.                     g_offscreen_GrafPort.portRect.bottom / int_scale );
  838.                 CopyBits( &g_offscreen_GrafPort.portBits,
  839.                     &g_offscreen_GrafPort.portBits,
  840.                     &g_offscreen_GrafPort.portRect, &dest_rect,
  841.                     srcCopy, nil );
  842.         }
  843.         else
  844.         {
  845.             SetRect( &dest_rect, 0, 0, 
  846.                  (int)((float)g_offscreen_GrafPort.portRect.right / scale),
  847.                 (int)((float)g_offscreen_GrafPort.portRect.bottom / scale) );
  848.             CopyBits( &g_offscreen_GrafPort.portBits, &g_offscreen_GrafPort.portBits,
  849.                 &g_offscreen_GrafPort.portRect, &dest_rect,
  850.                 srcCopy, nil );
  851.         }
  852.     }
  853.     
  854.     /* show contents of offscreen bitmap */
  855.     SetPort( g_page_window );
  856.     InvalRect( &g_page_window->portRect );
  857.     
  858.     SetPort( (GrafPtr) g_print_port_p );
  859.     dest_rect.left = 0;
  860.     dest_rect.right = g_offscreen_GrafPort.portRect.right;
  861.     page_bottom = (**g_print_rec_h).prInfo.rPage.bottom;
  862.     page_top = (**g_print_rec_h).prInfo.rPage.top;
  863.     strip_depth = (**g_print_rec_h).prXInfo.iBandV;
  864.     if (strip_depth < 8)
  865.     {
  866.         strip_depth = 3600;
  867.     }
  868.     if ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  869.         copies = 1; /* Pr. Man. handles copies in spool mode */
  870.     else
  871.         copies = (**g_print_rec_h).prJob.iCopies;
  872.     for (i = 0; i < copies; i++)
  873.     {
  874.         if (User_wants_out())
  875.             break;
  876.         PrOpenPage( g_print_port_p, nil );
  877.         if (PrError() != noErr)
  878.         {
  879.             printf("PrOpenPage error %d.\n", PrError());
  880.             /* (void)fflush(stdout); */
  881.         }
  882.         (**g_print_rec_h).prJob.pIdleProc = (ProcPtr)Printer_idle;
  883.         if ((**g_print_rec_h).prStl.feed == feedCut) /* refresh cancel dialog */
  884.         {
  885.             DrawDialog( cancel_dialog );
  886.             SetPort( (GrafPtr) g_print_port_p );
  887.         }
  888.         if ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  889.             /* Tell the ImageWriter driver not to */
  890.             PicComment(1000, 0, nil);    /* ..."thin the bits" */
  891.         if (PrError() == noErr)
  892.         {
  893.             for (dest_rect.top = page_top;
  894.                 dest_rect.top < page_bottom;
  895.                 dest_rect.top += strip_depth)
  896.             {
  897.                 dest_rect.bottom = MIN(dest_rect.top + strip_depth, page_bottom);
  898.                 rollbeachball();
  899.                 SetPort( (GrafPtr) g_print_port_p );
  900.                 CopyBits( &g_offscreen_GrafPort.portBits,
  901.                     &g_print_port_p->gPort.portBits,
  902.                     &dest_rect,
  903.                     &dest_rect,
  904.                     srcCopy, nil );
  905.             }
  906.         }
  907.         if ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  908.             PicComment(1001, 0, nil);
  909.         PrClosePage( g_print_port_p );
  910.         if (PrError() != noErr)
  911.         {
  912.             g_abort_dvi = TRUE;
  913.             printf("PrClosePage error %d.\n", PrError());
  914.             break;
  915.         }
  916.     }
  917.     SetPort( save_port_p );
  918. }
  919.  
  920. /* --------------------- User_wants_out -------------------- */
  921. Boolean
  922. User_wants_out()
  923. {
  924.     Printer_idle();
  925.     return( g_abort_dvi );
  926. }
  927.  
  928. /* ------------------- Printer_idle ---------------------- */
  929. pascal void
  930. Printer_idle()
  931. {
  932.     register  long        ticks;
  933.     GrafPtr    save_port;
  934.     
  935.     GetPort( &save_port );
  936.     rollbeachball();
  937.     ticks = TickCount();
  938.     if (ticks - g_last_idle_time > IDLE_INTERVAL)
  939.     {
  940.         Update_memory_indicators();
  941.         Handle_event();
  942.         g_last_idle_time = ticks;
  943.     }
  944.     SetPort( save_port );
  945. }
  946.  
  947. /* --------------------- Close_printer -------------------- */
  948. /* Called by dvifile */
  949. void
  950. Close_printer()
  951. {
  952.     TPrStatus stat;
  953.     
  954.     UnloadSeg( sprintf );
  955.     UnloadSeg( special );
  956.     PrCloseDoc( g_print_port_p );
  957.     SetPort( FrontWindow() );
  958.     if ( (PrError() != noErr) && (PrError() != iPrAbort) )
  959.     {
  960.         printf("\nPrCloseDoc error %d.\n", PrError());
  961.         /* (void)fflush(stdout); */
  962.     }
  963.     else
  964.         if ( ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  965.             && (PrError() != iPrAbort) )
  966.         {
  967.             SetIText( cancel_dialog_text, "\pNow Printing." );
  968.             DrawDialog( cancel_dialog );
  969.             (**g_print_rec_h).prJob.pIdleProc = (ProcPtr)Printer_idle;
  970.             PrPicFile( g_print_rec_h, nil, nil, nil, &stat );
  971.             if ( (PrError() != noErr) && (PrError() != iPrAbort) )
  972.             {
  973.                 printf("\nPrPicFile error %d.\n", PrError());
  974.                 /* (void)fflush(stdout); */
  975.             }
  976.         }
  977.     if (PrError() != noErr)
  978.         Explain_print_error( PrError() );
  979.     PrClose();
  980.     HideWindow( cancel_dialog );
  981.     SetPort ( FrontWindow() );
  982. }
  983.  
  984. #define    VERSION_ITEM    1
  985. #define    VERS_ID        1
  986.  
  987. typedef struct    vers {
  988.     unsigned char version_number;
  989.     unsigned char revision_number;
  990.     unsigned char revision_stage;
  991.     unsigned char build_number;
  992.     int    language_integer;
  993.     unsigned char string_data[];    /* two Pascal strings: abbreviated, GetInfo */
  994. } vers, **vers_h;
  995.  
  996. /* ----------------------- show_about() ---------------------------- */
  997. void
  998. show_about()
  999. {
  1000.     DialogPtr    about_dlog;
  1001.     int        type, hit;
  1002.     Handle    stat_h;
  1003.     Rect        box;
  1004.     vers_h    v_h;
  1005.     int    about_id = Get_resource_id('DLOG', "about box");
  1006.     
  1007.     about_dlog = GetNewDialog( about_id, nil, (WindowPtr)MOVE_TO_FRONT );
  1008.     GetDItem( about_dlog, VERSION_ITEM,  &type, &stat_h, &box );
  1009.     v_h = (vers_h) Get_resource_by_id('vers', VERS_ID);
  1010.     SetIText( stat_h, (**v_h).string_data );
  1011.     ShowWindow( about_dlog );
  1012.     TD_activate( FALSE );
  1013.     ModalDialog( nil, &hit );
  1014.     DisposDialog( about_dlog );
  1015.     ReleaseResource( (Handle)v_h );
  1016. } /* show_about */
  1017.  
  1018. /* ---------------------- Show_error ------------------------- */
  1019. void
  1020. Show_error( msg )
  1021. char *msg;
  1022. {
  1023.     char pmsg[256];
  1024.     
  1025.     (void) strncpy( pmsg, msg, (size_t) 255 );
  1026.     pmsg[255] = '\0';
  1027.     (void) CtoPstr( pmsg );
  1028.     ParamText( pmsg, "", "", "" );
  1029.     TD_activate(FALSE);
  1030.     StopAlert( Get_resource_id('ALRT', "error message"), nil );
  1031. }
  1032.  
  1033. /* ---------------------- Show_warning ------------------------- */
  1034. void
  1035. Show_warning( msg )
  1036. char *msg;
  1037. {
  1038.     char pmsg[256];
  1039.     
  1040.     (void) strncpy( pmsg, msg, (size_t) 255 );
  1041.     pmsg[255] = '\0';
  1042.     (void) CtoPstr( pmsg );
  1043.     ParamText( pmsg, "", "", "" );
  1044.     TD_activate(FALSE);
  1045.     CautionAlert( Get_resource_id('ALRT', "warning message"), nil );
  1046. }
  1047.  
  1048. /* ------------------------ Get_resource_id -------------------- */
  1049. int
  1050. Get_resource_id(
  1051.     OSType        resource_type,
  1052.     char        *resource_name)
  1053. {
  1054.     char rsrc_name[80];
  1055.     Handle resource_handle;
  1056.     int resource_id;
  1057.  
  1058.     (void) strcpy( rsrc_name, resource_name );
  1059.     (void) CtoPstr( rsrc_name );
  1060.     resource_handle = GetNamedResource( resource_type, rsrc_name );
  1061.     if (resource_handle == nil)
  1062.     {
  1063.         SysBeep(1);
  1064.         alldone(0);
  1065.     };
  1066.     GetResInfo( resource_handle, &resource_id, nil, nil );
  1067.     return( resource_id );
  1068. }
  1069.  
  1070. /* ------------------------ Get_resource ----------------------------- */
  1071. void
  1072. Get_resource( /* find a resource by name */
  1073.     OSType    resource_type,        /* given type... */
  1074.     char        *resource_name,    /* and name... */
  1075.     Handle    *resource_handle,    /* it returns handle... */
  1076.     int        *resource_id        /* and id. */
  1077.     )
  1078. {
  1079.     char rsrc_name[80];
  1080.     char temp[5];
  1081.     int    save_resfile;
  1082.  
  1083.     (void) strcpy( rsrc_name, resource_name );
  1084.     (void) CtoPstr( rsrc_name );
  1085.     save_resfile = CurResFile();
  1086.     UseResFile( g_app_resfile );
  1087.     *resource_handle = Get1NamedResource( resource_type, rsrc_name );
  1088.     UseResFile( save_resfile );
  1089.     if (*resource_handle == nil)
  1090.     {
  1091.         (void) strncpy( temp, (char *)&resource_type, 4 );
  1092.         temp[4] = '\0';
  1093.         (void) sprintf( rsrc_name,
  1094.             "Couldn't find resource '%s' of type '%s'!",
  1095.             resource_name, temp );
  1096.         Show_error(rsrc_name);
  1097.         alldone();
  1098.     };
  1099.     GetResInfo( *resource_handle, resource_id, nil, nil );
  1100. } /* Get_resource */
  1101.  
  1102. /* --------------------- Get_resource_by_id ---------------------- */
  1103. Handle
  1104. Get_resource_by_id( /* find a resource by ID */
  1105.     OSType    resource_type,        /* given type... */
  1106.     int        resource_id        /* and id. */
  1107.     )
  1108. {
  1109.     char rsrc_name[80];
  1110.     char temp[5];
  1111.     Handle    resource_handle;
  1112.     int    save_resfile;
  1113.  
  1114.     save_resfile = CurResFile();
  1115.     UseResFile( g_app_resfile );
  1116.     resource_handle = Get1Resource( resource_type, resource_id );
  1117.     UseResFile( save_resfile );
  1118.     if (resource_handle == nil)
  1119.     {
  1120.         (void) strncpy( temp, (char *)&resource_type, 4 );
  1121.         temp[4] = '\0';
  1122.         (void) sprintf( rsrc_name,
  1123.             "Couldn't find resource %d of type '%s'!",
  1124.             resource_id, temp );
  1125.         Show_error(rsrc_name);
  1126.         alldone();
  1127.     };
  1128.     return( resource_handle );
  1129. } /* Get_resource_by_id */
  1130.  
  1131. /* ------------------------- init_toolbox --------------------------- */
  1132. void init_toolbox()
  1133. {
  1134.     long n;
  1135.     Ptr p;
  1136.     
  1137.     p = GetApplLimit();
  1138.     p -= 20000; /* extra stack space */
  1139.     SetApplLimit(p);
  1140.     n = MemError();
  1141.     MaxApplZone();
  1142.     MoreMasters(); MoreMasters(); MoreMasters();
  1143.     InitGraf( (Ptr)&thePort );
  1144.     InitFonts();
  1145.     FlushEvents(everyEvent, 0);
  1146.     InitWindows();
  1147.     InitMenus();
  1148.     TEInit();
  1149.     InitDialogs(0L);
  1150.     InitCursor();
  1151. #if PROFILE
  1152.     InitProfile( 300, 300 );
  1153. #endif
  1154. } /* init_toolbox */
  1155.  
  1156. /* ---------------------- set_str_resource ------------------------- */
  1157. void
  1158. set_str_resource( char *rsrc_name, char *new_value )
  1159. {
  1160.     char    **r_handle;
  1161.     int     rsrc_id, new_len, i;
  1162.  
  1163.     /* Get a handle to the resource. */
  1164.     Get_resource( 'Cstr', rsrc_name, &r_handle, &rsrc_id );
  1165.     
  1166.     new_len = strlen( new_value );
  1167.     
  1168.     /* Resize the handle. */
  1169.     if (new_len + 1 != GetHandleSize( r_handle ))
  1170.         SetHandleSize( r_handle, new_len + 1 );
  1171.     if (MemError() != noErr)
  1172.         Show_warning("set_str_resource: SetHandleSize error");
  1173.     
  1174.     /* Copy the new value to the new handle. */
  1175.     (void) strcpy( *r_handle, new_value );
  1176.     
  1177.     /* Update the resource file. */
  1178.     ChangedResource( r_handle );
  1179.     if (ResError() != noErr)
  1180.         Show_warning("set_str_resource: ChangedResource error");
  1181.     WriteResource( r_handle );
  1182.     if (ResError() != noErr)
  1183.         Show_warning("set_str_resource: WriteResource error");
  1184. } /* set_str_resource */
  1185.  
  1186. /* ---------------------- set_int_resource ------------------------- */
  1187. void
  1188. set_int_resource( char *rsrc_name, int new_value )
  1189. {
  1190.     int    **r_handle;
  1191.     int     rsrc_id, new_len, i;
  1192.  
  1193.     /* Get a handle to the resource. */
  1194.     Get_resource( 'int ', rsrc_name, (Handle *)&r_handle, &rsrc_id );
  1195.     
  1196.     /* Copy the new value to the new handle. */
  1197.     **r_handle = new_value;
  1198.     
  1199.     /* Update the resource file. */
  1200.     ChangedResource( (Handle)r_handle );
  1201.     if (ResError() != noErr)
  1202.         Show_warning("set_int_resource: ChangedResource error");
  1203.     WriteResource( (Handle)r_handle );
  1204.     if (ResError() != noErr)
  1205.         Show_warning("set_int_resource: WriteResource error");
  1206. } /* set_int_resource */
  1207.  
  1208.  
  1209. /* ---------------------- get_bool_resource ----------------- */
  1210. Boolean
  1211. get_bool_resource( char *rsrc_name )
  1212. {
  1213.     Handle r_handle;
  1214.     int    rsrc_id;
  1215.  
  1216.     Get_resource( 'Bool', rsrc_name, &r_handle, &rsrc_id );
  1217.     return( **(Boolean **)r_handle );
  1218. } /* get_bool_resource */
  1219.  
  1220. /* ---------------------- get_int_resource ----------------- */
  1221. int
  1222. get_int_resource( char *rsrc_name )
  1223. {
  1224.     Handle r_handle;
  1225.     int        rsrc_id;
  1226.     
  1227.     Get_resource( 'int ', rsrc_name, &r_handle, &rsrc_id );
  1228.     return( **(int **)r_handle );
  1229. } /* get_int_resource */
  1230.  
  1231. /*----------------------- get_str_resource -------------------------*/
  1232. char **
  1233. get_str_resource( char *rsrc_name )
  1234. {
  1235.     Handle r_handle;
  1236.     int    rsrc_id;
  1237.     
  1238.     Get_resource( 'Cstr', rsrc_name, &r_handle, &rsrc_id );
  1239.     return( (char **) r_handle );
  1240. } /* get_str_resource */
  1241.  
  1242. /* ---------------------- Get_SF_place ----------------------- */
  1243. Point
  1244. Get_SF_place( int rsrc_id )
  1245. {
  1246.     Point where;
  1247.     DialogTHndl sfget_h;
  1248.     int sfget_width, sfget_depth;
  1249.  
  1250. /* Figure out how to center the SFGetFile dialog on the screen.*/
  1251.     sfget_h = (DialogTHndl) GetResource( 'DLOG', rsrc_id );
  1252.     if (ResError())
  1253.         Show_warning("SFGet resource error 1.");
  1254.     LoadResource( (Handle)sfget_h );
  1255.     if (ResError())
  1256.         Show_warning("SFGet resource error 2.");
  1257.     sfget_width = (**sfget_h).boundsRect.right
  1258.         - (**sfget_h).boundsRect.left;
  1259.     sfget_depth = (**sfget_h).boundsRect.bottom
  1260.         - (**sfget_h).boundsRect.top;
  1261.     where.h = (g_screenrect.right
  1262.         - g_screenrect.left - sfget_width) / 2
  1263.         + g_screenrect.left;
  1264.     where.v = (g_screenrect.bottom
  1265.         - g_screenrect.top - sfget_depth) / 3
  1266.         + g_screenrect.top;
  1267.     return( where );
  1268. }
  1269.  
  1270. /* ------------------------- Get_file_path -------------------------*/
  1271. void
  1272. Get_file_path( char *thepath )
  1273. {
  1274. Point    where;
  1275. SFReply reply;
  1276. int errcode;
  1277.  
  1278.     where = Get_SF_place( getDlgID );    
  1279.  
  1280. /* Put up standard file dialog box for the user to pick a file.  Show
  1281. only files of type ODVI or with names ending in ".dvi" or ".DVI" */
  1282.  
  1283.     SFGetFile(where, "\pfile:", Dvi_filter, -1, nil, 0L, &reply);
  1284.     
  1285.     if (reply.good)
  1286.     {
  1287.         (void) PtoCstr( (char *)reply.fName );
  1288.         (void) strcpy( thepath, (char *)reply.fName );
  1289.         errcode = SetVol( nil, reply.vRefNum );
  1290.         if (errcode != noErr)
  1291.             printf("SetVol error %d.\n", errcode );
  1292.     }
  1293.     else
  1294.     {
  1295.         (void)strcpy( thepath, "" );
  1296.     }
  1297. } /* Get_file_path */
  1298.  
  1299. /* ---------------------------- Dvi_filter --------------------------- */
  1300.  
  1301. pascal Boolean Dvi_filter( ParmBlkPtr param_block )
  1302. {
  1303.     static char suffix[5] = ".dvi";
  1304.     char *fname_ptr;
  1305.     char fname[64];
  1306.     int fname_len, i;
  1307.     
  1308.     if ( (*param_block).fileParam.ioFlFndrInfo.fdType == 'ODVI' )
  1309.         return( FALSE ); /* Display files of type ODVI */
  1310.     fname_ptr = (char *)(*param_block).fileParam.ioNamePtr;
  1311.     fname_len = *fname_ptr;
  1312.     if (fname_len < 4)
  1313.         return( TRUE ); /* can't end in ".dvi" with fewer than 4 bytes */
  1314.     for (i=0; i < fname_len; i++)
  1315.         fname[i] = *(++fname_ptr);
  1316.     fname[ fname_len ] = '\0';
  1317.     i = 3; /* strlen(suffix) - 1 */
  1318.     fname_len = fname_len - 1;
  1319.     while ( (i >= 0) && (tolower(fname[fname_len]) == suffix[i]) )
  1320.     {
  1321.         --fname_len;
  1322.         --i;
  1323.     };
  1324.     return( i >= 0 );
  1325. }
  1326.  
  1327.  
  1328. void
  1329. init_per_dvi()            /* initialize global variables */
  1330. {
  1331.     register INT16 i;        /* loop index */
  1332.  
  1333. /***********************************************************************
  1334.     Set up masks such that rightones[k]  has 1-bits at the right end  of
  1335.     the word from k ..     HOST_WORD_SIZE-1, where bits are numbered  from
  1336.     left (high-order) to right (lower-order), 0 .. HOST_WORD_SIZE-1.
  1337.  
  1338.     img_mask[k] has  a 1-bit  in  position k,  bits numbered  as  above.
  1339.     power[k] is  1  <<  k,  and gpower[k]  is  2**k-1  (i.e.  1-bits  in
  1340.     low-order k positions).  These 3 require only 32 entries each  since
  1341.     they deal with 32-bit words from the PK and GF font files.
  1342.  
  1343.     These are set  at run-time, rather  than compile time  to avoid  (a)
  1344.     problems with C  preprocessors which sometimes  do not handle  large
  1345.     constants correctly, and (b) host byte-order dependence.
  1346. ***********************************************************************/
  1347.  
  1348.  
  1349.     debug_code = 0;
  1350.  
  1351.     npage = 0;
  1352.     topmargin = 1.0;            /* DVI driver standard default */
  1353.     leftmargin = 1.0;            /* DVI driver standard default */
  1354.  
  1355.     subfile[0] = '\0';
  1356.  
  1357. #if    VIRTUAL_FONTS
  1358.     virt_font = FALSE;
  1359. #endif
  1360.  
  1361. }
  1362.